//valida telefone
function ValidaTelefone(tel){
    exp = /\(\d{2}\)\ \d{4}\-\d{4}/
    if(!exp.test(tel.value)) {
        alert('Numero de Telefone Invalido!');
        return false;
    }
    
    return true;
}

//valida CEP
function ValidaCep(cep){
    exp = /\d{5}\-\d{3}/
    if(!exp.test(cep.value)) {
        alert('Numero de Cep Invalido!');
        return false;
    }
    
    return true;
}

//valida data
function ValidaData(data){
    exp = /\d{2}\/\d{2}\/\d{4}/
    if(!exp.test(data.value))
        alert('Data Invalida!');
}

//valida o CPF digitado
function ValidarCPF(Objcpf){
    cpf = Objcpf.value;
    cpf = cpf.replace(".", "");
    cpf = cpf.replace(".", "");
    cpf = cpf.replace("-", "");
    erro = new String;
    if (cpf.length < 11 && cpf.length > 0) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n";
    var nonNumbers = /\D/;
    if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
//    cpf == "00000000000" || 
    if (cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
        erro += "Numero de CPF invalido!"
    }
    var a = [];
    var b = new Number;
    var c = 11;
    for (i=0; i<11; i++){
        a[i] = cpf.charAt(i);
        if (i < 9) b += (a[i] * --c);
    }
    if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
    b = 0;
    c = 11;
    for (y=0; y<10; y++) b += (a[y] * c--);
    if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
    if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
        erro +="CPF invalido!";
    }
    if (erro.length > 0){
        alert(erro);
        return false;
    }
    return true;
}

//valida o CNPJ digitado
function ValidarCNPJ(ObjCnpj){
    var cnpj = ObjCnpj.value;
    var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
    var dig1= new Number;
    var dig2= new Number;

    exp = /\.|\-|\//g
    cnpj = cnpj.toString().replace( exp, "" );
    var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));

    for(i = 0; i<valida.length; i++){
        dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0);
        dig2 += cnpj.charAt(i)*valida[i];
    }
    dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
    dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));

    if(((dig1*10)+dig2) != digito)
        return false;
        
	return true;
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;

	if (formField.value == "")
	{
		alert('Por favor, entre com um valor para o campo "' + fieldLabel +'".');
		formField.focus();
		result = false;
	}

	return result;
}

function isEmpty(formField,fieldLabel)
{
	var result = false;

	if (formField.value == "")
	{
		result = true;
	}

	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}

	return result;
}

function validEmail(formField,fieldLabel,required)
{
        if(!required && formField.value.length == 0) {
            return true;
        }
    
        if (required && !validRequired(formField,fieldLabel))
		return false;

	if (((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Por favor, entre com um e-mail completo na forma: fulano@dominio.com");
		formField.focus();
		return false;
	}

        return true;
}

function validDate(formField,fieldLabel,required)
{
	var result = true;
	var diaValido = true;
	var mesValido = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result)
 	{
 		var elems = formField.value.split("/");

 		result = (elems.length == 3); // should be three components

 		if (result)
 		{
 			var day = parseInt(elems[0],10);
 			var month = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (day > 0) && (day < 32) &&
					 allDigits(elems[1]) && (month > 0) && (month < 13) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));

                        mesValido = allDigits(elems[1]) && (month > 0) && (month < 13);

                        if(mesValido) {
                          if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) {
                            diaValido = allDigits(elems[0]) && (day > 0) && (day < 32);
                          }
                          else if ((month == 2) && ((year%4 == 0) && ((year%100 != 0) || (year%100 == 0 && year%400 == 0)))) { //Ano bissexto
                            diaValido = allDigits(elems[0]) && (day > 0) && (day < 30);
                          }
                          else if ((month == 2) && !((year%4 == 0) && ((year%100 != 0) || (year%100 == 0 && year%400 == 0)))) { //Ano bissexto
                              diaValido = allDigits(elems[0]) && (day > 0) && (day < 29);
                          }
                          else {
                            diaValido = allDigits(elems[0]) && (day > 0) && (day < 31);
                          }
                        }
                }

  		if (!result) {
                    alert('A data deve ser no formato DD/MM/YYYY para o campo "' + fieldLabel +'".');
                    formField.focus();
                    return false;
		}

                if (!diaValido) {
                        alert('Dia informado invalido.');
                        formField.focus();
                        return false;
                }

                if (!mesValido) {
                        alert('Mes informado invalido.');
                        formField.focus();
                        return false;
                }
        }

	return true;
}